/*
 * ================================================================================变量定义区
 */
var optype = ""; //定义操作类型为 create/update
var currentPage = 1;//全局变量，当前页
var validateRules = "";//校验规则
												
/*
 * ================================================================================页面初始化函数
 */
$(function()
{
	//获取选项编号
	var paramKey = $("#hiddenParamKey").val();
	if(paramKey && paramKey.length>0)
	{
		optype = "update";
		$("#pageName").empty().append("update param");
		$("#paramKey").attr("disabled","disabled");		
	}
	else
	{
		optype = "create";
		$("#pageName").empty().append("create param");
	}
	
	//加载验证规则
	setRules();
	
	//启动验证
	$("#systemParamForm").validate(validateRules);
	
	//绑定事件
	$("#btnSubmit").bind('click',function()
	{
		submitData();
	});
	$("#btnCancel").bind('click',function()
	{
		cancel();
	});	
});


/*
 * ================================================================================函数定义区
 * 
 */


/**
 * 设置验证规则
 */
function setRules()
{
	validateRules = 
	{
		rules: 
		{
			paramKey:
			{
				required:true,
				paramKeyMaxLength:true,
				remote:
				{
					type:"post",
					url:BASE_PATH+"/systemParamManagement/checkParamKey.do",
					async: false,
					data:
					{
						paramKey:function()
		                 {
		                	 return $("#paramKey").val().trim()+","+optype;
		                 }
		             }  
		        },
			},
			
			paramVal:
			{
				required:true,
				paramValMaxLength:true,
			},
			
			paramDesc:
			{
				required:false,
				paramDescMaxLength:true,
			},
		},
		
		messages:
		{
			paramKey:
			{
				required:"请输入PARAM_KEY",
				remote:jQuery.format("PARAM_KEY已存在,请重新输入"),
				paramKeyMaxLength:jQuery.format("PARAM_KEY过长,请重新输入")
			},
			
			paramVal:
			{
				required:"请输入PARAM_VAL",
				paramVal:jQuery.format("PARAM_VAL过长,请重新输入")
			},
			
			paramDesc:
			{
				paramDescMaxLength:jQuery.format("PARAM_DESC过长,请重新输入")
			},
		}
	};	
	
	//验证paramKey长度是否符合要求
	$.validator.addMethod("paramKeyMaxLength",function(value,element)
	{
		var name = $("#paramKey").val().trim();
		var totalLength = checkStringLength(name);
		var result = true;
		
		//判断长度是否大于数据库长度
		if(totalLength-32>0)
		{
			result = false;
		}
		
		return result;
	}
	,"PARAM_KEY过长,请重新输入");
	
	//验证paramVal长度是否符合要求
	$.validator.addMethod("paramValMaxLength",function(value,element)
	{
		var name = $("#paramVal").val().trim();
		var totalLength = checkStringLength(name);
		var result = true;
		
		//判断长度是否大于数据库长度
		if(totalLength-128>0)
		{
			result = false;
		}
		
		return result;
	}
	,"PARAM_VAL过长,请重新输入");
	
	//验证paramDesc长度是否符合要求
	$.validator.addMethod("paramDescMaxLength",function(value,element)
	{
		var name = $("#paramDesc").val().trim();
		var totalLength = checkStringLength(name);
		var result = true;
		
		//判断长度是否大于数据库长度
		if(totalLength-128>0)
		{
			result = false;
		}
		
		return result;
	}
	,"PARAM_DESC过长,请重新输入");
	
	
}

/**
 * 提交按钮事件
 */
function submitData()
{
	
	//提交时再进行验证
	var validateResult = $("#systemParamForm").validate(validateRules).form();
	
	//判断验证是否通过
	if(!validateResult)
	{
		openAlert("请按提示填写完整信息");
		return;
	}
	else
	{
	     var paramKey	=$("#paramKey").val().trim();	    
	     var paramVal	=$("#paramVal").val().trim();
	     var paramLevel	=$("#paramLevel").val();
	     var paramDesc	=$("#paramDesc").val().trim();
	     var createUser =$("#singleIdHidden").attr("value");
		$.ajax(
		{
			type: 'POST',
	        url: BASE_PATH +'/systemParamManagement/addOrUpdateSystemParam.do',
	        dataType:"json",       	       
			data:
			{
				"createUser"               :	createUser,
				"lastModifyUser"           :	createUser,
				"paramKey"                 :	paramKey,
				"paramVal"				   :	paramVal,
				"paramLevel"			   :	paramLevel,
				"paramDesc"				   :	paramDesc,
				"optype"   				   :    optype
			},
			
			success : function(data) 
			{				
				if(data=='success')
				{
					openAlert('保存成功',function()
					{
						backToCurrentPage();
					});
				}
				else
				{
					openAlert('保存失败');
				}				
			},
			
			error:function(e)
			{
				openAlert('操作失败！错误信息：<div>'+e.responseText+"</div>");
			}
		});
	}
}

/**
 * 返回按钮事件
 */
function cancel()
{
	openConfirm('确定取消吗？', null, function() 
	{
		backToCurrentPage();
	});	
}

/**
 * 返回当前页
 */
function backToCurrentPage()
{
	$("#submitcurrentpage").submit();
}

/**
 * 获取输入字符串的unicode长度
 * @param name 输入的字符串
 * @returns totalLength 字符串的unicode长度
 */
function checkStringLength(name)
{
	var totalLength = 0;
	var charCode = 0; 
	
	for (var i = 0; i < name.length; i++) 
	{ 
		charCode = name.charCodeAt(i);
		
		if (charCode < 0x007f) 
		{ 
			totalLength = totalLength + 1; 
		} 
		else if ((0x0080 <= charCode) && (charCode <= 0x07ff)) 
		{ 
			totalLength += 2; 
		}
		else if ((0x0800 <= charCode) && (charCode <= 0xffff))
		{ 
			totalLength += 3; 
		} 
	} 
	return totalLength;
}
